#### Start of system configuration section. ####

srcdir =	@srcdir@

ifeq ($(srcdir),.)
srcdir = ../..
endif

VPATH :=	$(srcdir)

# Common prefix for machine-independent installed files.
prefix =	@prefix@

# Common prefix for machine-dependent installed files.
exec_prefix =	@exec_prefix@

bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/Sys/libs

INSTALL =	@INSTALL@
INSTALL_DATA =	@INSTALL_DATA@

CC =		@CC@

CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@

RANLIB =	@RANLIB@
AR =		ar

#### End system configuration section ####

ifdef CATENATE
DEBUG_FLAG =	-g
else
DEBUG_FLAG =	
endif

# I *love* GNU make!
define catenate
/bin/echo -n creating $@...
/bin/echo "$(^:%=#include \"%\"\n)" >$@
/bin/echo done
endef

FLAVOR_CFLAGS =	-m$(CPU) -m$(FPU)
ALL_CFLAGS =	$(CFLAGS) $(FLAVOR_CFLAGS) $(OTHER_CFLAGS) $(DEBUG_FLAG) $(INCS) $(DEFS)

DEFS =
INCS =		-I$(srcdir) -I$(srcdir)/../library -I$(srcdir)/../include
LIB =		library.a

# Strip the executable (-s), don't look for standard libraries (-nostdlib)
# don't link in crt0.o (-nostartfiles) and tell the linker explicitly to
# load the module from the library that defines the symbol "___load_seg"
# (-Xlinker -u -Xlinker ___load_seg).

LINKFLAGS =	-nostdlib -nostartfiles -Xlinker -u -Xlinker ___load_seg

.SUFFIXES:	.ot

.s.o:
		cp $< x.c
		$(CC) $(ALL_CFLAGS) -traditional -E x.c -o x.s
		$(CC) $(ALL_CFLAGS) -c x.s -o $@
		rm -f x.c x.s

.s.ot:
		cp $< x.c
		$(CC) $(ALL_CFLAGS) -traditional -DTRACE_LIBRARY -E x.c -o x.s
		$(CC) $(ALL_CFLAGS) -c x.s -o $@
		rm -f x.c x.s

.c.o:
		$(CC) $(ALL_CFLAGS) -c $< -o $@

all:		ixemul.library ixemul.trace ixemul.debug

SRC =		__amiga_filehandle.c __close.c __fioctl.c __fselect.c __fstat.c \
		__init_std_packet.c __ioctl.c __ioerr_to_errno.c __load_seg.c \
		__lock.c __make_link.c __mclose.c __mread.c __must_recompile.c \
		__open.c __plock.c __read.c __swrite.c __tioctl.c __unlock.c \
		__wait_packet.c __write.c _cli_parse.c _wb_parse.c access.c \
		buddy-alloc.c chdir.c chmod.c close.c closedir.c convert_dir.c \
		creat.c createextio.c createport.c createstdio.c createtask.c \
		deleteextio.c deleteport.c deletestdio.c deletetask.c \
		fchdir.c fchmod.c fsync.c ftime.c ftruncate.c get_file.c getcrap.c \
		getpriority.c getrlimit.c gettimeofday.c hwck.c ioctl.c isatty.c \
		ix_close.c ix_expunge.c ix_get_vars2.c ix_init.c ix_open.c \
		ix_panic.c ix_settings.c ix_sigwinch.c ix_sleep.c ix_stubs.c \
		kern_descrip.c kern_sig.c kern_time.c kmalloc.c link.c lseek.c \
		malloc.c mathsup.c mkdir.c open.c opendir.c pipe.c \
		posixconf.c profil.c ptrace.c read.c readdir.c readlink.c readv.c \
		rename.c rewinddir.c rmdir.c seekdir.c select.c setpriority.c \
		setrlimit.c settimeofday.c smallfuncs.c socket.c \
		stat.c statfs.c symlink.c telldir.c truncate.c umask.c unlink.c \
		utime.c utimes.c vfork.c write.c writev.c

OBJ =		$(SRC:.c=.o)

SRC_A4 =	ix_startup.c _main.c ix_exec_entry.c exit.c ix_resident.c machdep.c
OBJ_A4 =	$(SRC_A4:.c=.o)

OBJ_OTHERS =	trap.o ix_timer.o debugstub.o version.o

SRC___ =	$(filter _%,$(SRC))
SRC_AH =	$(filter a% b% c% d% e% f% g% h%,$(SRC))
SRC_II =	$(filter i%,$(SRC))
SRC_JO =	$(filter j% k% l% m% n% o%,$(SRC))
SRC_PS =	$(filter p% q% r% s%,$(SRC))
SRC_TZ =	$(filter t% u% v% w% x% y% z%,$(SRC))

# These are object files which explicitly get linked into the ixemul.library
# file, in the order specified

OBJ_N =		start.o tracecntl.o

OBJ_T =		start.ot tracecntl.ot

IXLIBS =	$(LIB) \
		../../../string/$(CPU)/$(FPU)/libstring.a \
		../../../general/$(CPU)/$(FPU)/libgeneral.a \
		../../../stdlib/$(CPU)/$(FPU)/libstdlib.a \
		../../../stdio/$(CPU)/$(FPU)/libstdio.a

LIBS =		-lamiga

ifdef CATENATE
ALL =		a4.o __.o ah.o ii.o jo.o ps.o tz.o $(OBJ_OTHERS)
else
ALL =		$(OBJ) $(OBJ_A4) $(OBJ_OTHERS)
endif

$(LIB) :	$(ALL)
		rm -f $@
		$(AR) rv $@ $(ALL)
		$(RANLIB) $@

ixemul.library:	$(OBJ_N) $(IXLIBS)
		$(CC) -s $(LINKFLAGS) $(OBJ_N) $(IXLIBS) $(LIBS) -o $@

ixemul.trace:	$(OBJ_T) $(IXLIBS)
		$(CC) -s $(LINKFLAGS) $(OBJ_T) $(IXLIBS) $(LIBS) -o $@

ixemul.debug:	$(OBJ_N) $(IXLIBS)
		$(CC) -g $(LINKFLAGS) $(OBJ_N) $(IXLIBS) $(LIBS) -o $@

clean:
		rm -rf 680?0

clobber:	clean
		rm -f Makefile

$(SRC_A4:.c=.o) a4.o:	%.o: %.c
		$(CC) $(ALL_CFLAGS) -ffixed-a4 -c $< -o $@

a4.c:		$(SRC_A4)
		@$(catenate)

__.c:		$(SRC___)
		@$(catenate)

ah.c:		$(SRC_AH)
		@$(catenate)

ii.c:		$(SRC_II)
		@$(catenate)

jo.c:		$(SRC_JO)
		@$(catenate)

ps.c:		$(SRC_PS)
		@$(catenate)

tz.c:		$(SRC_TZ)
		@$(catenate)

start.o:	start.s $(srcdir)/version.h $(srcdir)/../include/sys/syscall.def ix_internals.h

start.ot:	start.s $(srcdir)/version.h $(srcdir)/../include/sys/syscall.def ix_internals.h

# this depends on the size of struct user (besides files that need field
# from struct user)

ix_open.o:	$(srcdir)/../include/user.h

# Force -m68020 & -m68881 so the assembler won't bitch about fpu
# instructions.  This means we have to leave out FLAVOR_CFLAGS.

trap.o:		trap.s ix_internals.h
		cp $< x.c
		$(CC) $(CFLAGS) $(OTHER_CFLAGS) $(INCS) -traditional -E x.c -o x.s
		$(CC) $(CFLAGS) $(OTHER_CFLAGS) $(INCS) -c -m68020 -m68881 x.s -o $@
		rm -f x.s x.c

ix_timer.o:	ix_timer.c
		$(CC) $(INCS) -O2 -fno-omit-frame-pointer -m68000 -msoft-float -c $< -o $@

version.o:	$(srcdir)/version.c

tracecntl.o:	tracecntl.c
		$(CC) $(ALL_CFLAGS) -ffixed-a4 -c $< -o $@

tracecntl.ot:	tracecntl.c
		$(CC) $(ALL_CFLAGS) -DTRACE_LIBRARY -ffixed-a4 -c $< -o $@

ix_internals.h:	create_header
		create_header >$@

create_header:	create_header.o

create_header.o:	create_header.c $(srcdir)/../include/user.h ixemul.h

$(OBJS):	ixemul.h
$(OBJS_N):	ixemul.h
$(OBJS_T):	ixemul.h

ix_settings.c vfork.c start.s:	$(srcdir)/version.h

$(srcdir)/version.h $(srcdir)/version.c:	parse_version ../version.in
			parse_version $(srcdir)
